var config = require("config");
var dialogManager = require("dialogManager");

cc.Class({
    
    extends: cc.Component,

    properties:()=>({
        img_role : {
            default : null,
            type : cc.Node
        },
        btn_rank : {
            default : null,
            type : cc.Node
        },
        img_bg : {
            default : null,
            type : cc.Node
        },
    }),
    onLoad: function () {
        // config.loadImage(this.img_bg,"first_bg","resources/image/");
        cc.vv.audioMgr.playBGM("background2");
        this.roleIndex = config.CurHeroIndex;
        this.haveHeros=[1,2,3,4,5];

        config.HeroMaxHp = config.heroInfo["hero"+this.roleIndex].hp*100;
        config.BulletHpDrop = config.heroInfo["hero"+this.roleIndex].hit*200;
        
        console.log("config.heroInfo:",config.heroInfo)
        // if(this.roleIndex != 1){
            config.loadImage(this.img_role,"char_"+this.roleIndex,"resources/image/");
        // }
        
        if(config.gotoUpLv){
            config.gotoUpLv = false;
            dialogManager.showRoleDialog();
        }
        config.LaserNum = 0;

        console.log(" cc.sys.os = ",cc.sys.os);
        // if (cc.sys.OS_ANDROID == cc.sys.os){
        //     jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity","exitGame","()V");
        // } 
        if (cc.sys.OS_ANDROID == cc.sys.os && config.Publish == "android"){
            this.btn_rank.active = false;
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity","showBanner","()V");
        }
    },
    start(){
        this.getUserShareInfo();
        if (cc.sys.OS_ANDROID == cc.sys.os && config.Publish == "android"){
            cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        } 
    },
    onDestroy () {
        if (cc.sys.OS_ANDROID == cc.sys.os && config.Publish == "android"){
            cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        }
    },
    onKeyDown (event) {
        switch(event.keyCode) {
            case cc.KEY.back:
                console.log("Android 返回建")
                //第一个是要调用的方法所在的安卓项目的包名，第二个是方法名，第三个是方法
                jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity","exitGame","()V");
                break;
        }
    },
    preloadNextScene(){
        dialogManager.showLoadingDialog();
        cc.director.preloadScene("gameScene", function () {
            cc.director.loadScene("gameScene");
        });
    },
    btnStarClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        var self = this;
        console.log("this.roleIndex=",this.roleIndex)
        if(!config.heroInfo["hero"+this.roleIndex].locked){
            config.CurHeroIndex = this.roleIndex;
            this.preloadNextScene();
        }else{
            //钱不够解锁
            var enterCall = function(){ 
                if(config.PlayerGold < config.heroInfo["hero"+self.roleIndex].monney){
                    // console.log(">>>>>>金币不够",config.heroInfo["hero"+self.roleIndex].monney);
                    if(typeof(wx)!="undefined"){
                        dialogManager.showNotEnoughGoldDialog();
                    }
                }else{
                    config.heroInfo["hero"+self.roleIndex].locked = false;
                    cc.vv.gameDataMgr.saveHeroInfo(self.roleIndex);
                    config.PlayerGold = config.PlayerGold - config.heroInfo["hero"+self.roleIndex].monney;
                    cc.vv.gameDataMgr.saveGold(config.PlayerGold);
                    // self.gold.string = config.PlayerGold;
                }
            }
            var txt = "解锁需要花费金币"+config.heroInfo["hero"+this.roleIndex].monney;
            if(config.PlayerGold < config.heroInfo["hero"+this.roleIndex].monney){
                txt = "金币不够"+config.heroInfo["hero"+this.roleIndex].monney+"无法解锁";
            }

            var args = {
                title:"温馨提示",
                content:txt,
                enterCallback:enterCall,
                cancelCallback:null,
            }
            dialogManager.showCommonDialog(args);
        }
    },
    btnRoleClick(){//角色
        cc.vv.audioMgr.playSFX("ui_button");
        dialogManager.showRoleDialog();
    },
    btnPetClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        dialogManager.showPetDialog();
    },
    btnSetClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        dialogManager.showSetDialog();
    },
    btnRankClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        dialogManager.showRankDialog();
    },
    btnLeftClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        if(this.roleIndex > 1){
            this.roleIndex--;
            this.showChar(this.haveHeros[this.roleIndex-1]);
        }
    },
    btnRightClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        if(this.roleIndex < this.haveHeros.length){
            this.roleIndex++;
            this.showChar(this.haveHeros[this.roleIndex-1]);
        }
    },
    showChar(index){
        console.log("index:",index);
        config.loadImage(this.img_role,"char_"+index,"resources/image/");
        config.CurHeroIndex = index;
        config.HeroMaxHp = config.heroInfo["hero"+index].hp*100;
        config.BulletHpDrop = config.heroInfo["hero"+index].hit*200;
    },
    //获取用户分享信息
    getUserShareInfo(){
        if(typeof(wx)=="undefined"){return;}
        var self = this;
        var launch = wx.getLaunchOptionsSync();
        self.shareUid =  launch.query.key;
        console.log("shareUid =  " ,self.shareUid,typeof(self.shareUid));
        if(self.shareUid == "undefined" || typeof(self.shareUid) == "undefined"){
            console.log("没有分享用户进入")
            self.shareUid = "";
        }
        var _url = "https://kg.51864.com/gemen/login.php";
        var params = {};
        params.openid = config.weiXinCode;
        params.linkid = self.shareUid;
        var fnRequest = function(){
            var xhr = cc.vv.http.sendRequest("",params,function(ret){
                console.log("ret = ",ret.gold);
                self.showGetGoldDialog(ret.gold);
            },_url);    
        };
        fnRequest();
    },
    showGetGoldDialog(gold){
        gold = parseInt(gold)
        if(gold>0){
        }else{
            console.log("gold:",gold);
            return;
        }

        var enterCall = function(){ 
            config.PlayerGold = config.PlayerGold+gold;
            cc.vv.gameDataMgr.saveGold(config.PlayerGold);
        }
        var txt = "发财了发财了！！恭喜您获得好友团赞助的"+gold+"金币!"
        var args = {
            title:"温馨提示",
            content:txt,
            enterCallback:enterCall,
            cancelCallback:null,
        }
        dialogManager.showCommonDialog(args);
    }
});
